<?php
	$con = mysqli_connect("localhost", "root", "", "dbtest");

	// Check connection
	if (mysqli_connect_errno()) {
		header("location:../error.php?err=1");
	} else {
		$product_code = "";
		$product_name = "";
		$quantity = 0;
		
		$single_err_flg = false;
		
		if (isset($_POST['txtProductCode'])) {
			$product_code = $_POST['txtProductCode'];
		}
		
		if (isset($_POST['txtProductName'])) {
			$product_name= $_POST['txtProductName'];
		}
		
		if (isset($_POST['txtQuantity'])) {
			$quantity = intval($_POST['txtQuantity']);
		}
		
		if (empty($product_code) && empty($product_name)) {
			$single_err_flg = true;
		}
		
		// case is not has single error
		if ($single_err_flg == false) {
			$sql = "Insert into products (code, name, quantity) Values ('$product_code', '$product_name', '$quantity')";
			
			if (!mysqli_query($con, $sql)) {
				die ("Error: " . mysqli_error($con));
			} else {
				echo "1 record added";
			}
		}
	}
	
	// close connection
	mysqli_close($con);
?>